File indexing completed on 2022-11-29 18:38:54

0001 /*
0002     KBlackBox - A simple game inspired by an emacs module
0003 
0004     SPDX-FileCopyrightText: 1999-2000 Robert Cimrman <cimrman3@students.zcu.cz>
0005     SPDX-FileCopyrightText: 2007 Nicolas Roffet <nicolas-kde@roffet.com>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #include "kbbgraphicsitemborder.h"
0011 
0012 
0013 
0014 #include "kbbscalablegraphicwidget.h"
0015 
0016 
0017 
0018 //
0019 // Constructor / Destructor
0020 //
0021 
0022 KBBGraphicsItemBorder::KBBGraphicsItemBorder(int borderPosition, int columns, int rows, float offset)
0023 {
0024     m_offset = offset;
0025     
0026     setSize(borderPosition, columns, rows);
0027 }
0028 
0029 
0030 
0031 //
0032 // Public
0033 //
0034 
0035 void KBBGraphicsItemBorder::setSize(int borderPosition, int columns, int rows)
0036 {
0037     m_borderPosition = borderPosition;
0038     m_columns = columns;
0039     m_rows = rows;
0040     
0041     centerCoordinate(m_borderPosition, m_centerX, m_centerY, m_offset);
0042 }
0043 
0044 
0045 
0046 //
0047 // Protected
0048 //
0049 
0050 void KBBGraphicsItemBorder::setBorderPosition(int borderPosition)
0051 {
0052     setSize(borderPosition, m_columns, m_rows);
0053 }
0054 
0055 
0056 void KBBGraphicsItemBorder::centerCoordinate(int borderPosition, float &centerX, float &centerY, float offset)
0057 {
0058     const float b = (float) KBBScalableGraphicWidget::BORDER_SIZE;
0059     const float r = (float) KBBScalableGraphicWidget::RATIO;
0060     float x;
0061     float y;
0062     if (borderPosition<m_columns) {
0063         x = borderPosition*r + b;
0064         y = offset;
0065     } else if (borderPosition<m_columns + m_rows) {
0066         x = m_columns*r + b + b/2 - offset;
0067         y = (borderPosition - m_columns)*r + b;
0068     } else if (borderPosition<2*m_columns + m_rows) {
0069         x = (2*m_columns + m_rows - borderPosition)*r + b/2;
0070         y = m_rows*r + 3*b/2 - offset;
0071     } else {
0072         x = offset;
0073         y = (2*m_columns + 2*m_rows - borderPosition)*r + b/2;
0074     }
0075     
0076     centerX = x + r/2;
0077     centerY = y + r/2;
0078 }
0079 
0080 
0081 float KBBGraphicsItemBorder::centerX() const
0082 {
0083     return m_centerX;
0084 }
0085 
0086 
0087 float KBBGraphicsItemBorder::centerY() const
0088 {
0089     return m_centerY;
0090 }
0091 
0092 
0093 int KBBGraphicsItemBorder::rotationAngle()
0094 {
0095     if (m_borderPosition<m_columns) {
0096         return 0;
0097     } else if (m_borderPosition<m_columns + m_rows) {
0098         return 90;
0099     } else if (m_borderPosition<2*m_columns + m_rows) {
0100         return 180;
0101     } else {
0102         return 270;
0103     }
0104 }