A simple yet powerful library for controlling graphical displays. Multiple display controllers are supported using inheritance.

Dependents:   mbed_rifletool Hexi_Bubble_Game Hexi_Catch-the-dot_Game Hexi_Acceleromagnetic_Synth

NOTE: This library is in beta right now. As far as I know, everything here works, but there are many features that are lacking so far. Most notably containers, button handling, and display drivers other than the SSD1306.

Committer:
neilt6
Date:
Tue May 27 21:41:28 2014 +0000
Revision:
3:a8f72d4864e6
Parent:
1:f7003ec66a51
Syntax improvements

Who changed what in which revision?

UserRevisionLine numberNew contents of line
neilt6 0:b876cf091464 1 /* NeatGUI Library
neilt6 0:b876cf091464 2 * Copyright (c) 2013 Neil Thiessen
neilt6 0:b876cf091464 3 *
neilt6 0:b876cf091464 4 * Licensed under the Apache License, Version 2.0 (the "License");
neilt6 0:b876cf091464 5 * you may not use this file except in compliance with the License.
neilt6 0:b876cf091464 6 * You may obtain a copy of the License at
neilt6 0:b876cf091464 7 *
neilt6 0:b876cf091464 8 * http://www.apache.org/licenses/LICENSE-2.0
neilt6 0:b876cf091464 9 *
neilt6 0:b876cf091464 10 * Unless required by applicable law or agreed to in writing, software
neilt6 0:b876cf091464 11 * distributed under the License is distributed on an "AS IS" BASIS,
neilt6 0:b876cf091464 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
neilt6 0:b876cf091464 13 * See the License for the specific language governing permissions and
neilt6 0:b876cf091464 14 * limitations under the License.
neilt6 0:b876cf091464 15 */
neilt6 0:b876cf091464 16
neilt6 0:b876cf091464 17 #include "Control.h"
neilt6 0:b876cf091464 18
neilt6 0:b876cf091464 19 Control::Control(int x, int y, int w, int h)
neilt6 0:b876cf091464 20 {
neilt6 0:b876cf091464 21 m_X = x;
neilt6 0:b876cf091464 22 m_Y = y;
neilt6 0:b876cf091464 23 m_Width = w;
neilt6 0:b876cf091464 24 m_Height = h;
neilt6 0:b876cf091464 25 m_Margin = 0;
neilt6 0:b876cf091464 26 m_Border = 0;
neilt6 0:b876cf091464 27 m_Padding = 0;
neilt6 0:b876cf091464 28 m_FgColor = 0xFFFFFFFF;
neilt6 0:b876cf091464 29 m_BgColor = 0xFF000000;
neilt6 0:b876cf091464 30 m_Text = NULL;
neilt6 0:b876cf091464 31 m_Font = NULL;
neilt6 0:b876cf091464 32 m_Invalid = true;
neilt6 0:b876cf091464 33 }
neilt6 0:b876cf091464 34
neilt6 1:f7003ec66a51 35 void Control::paint(Canvas* canvas)
neilt6 0:b876cf091464 36 {
neilt6 0:b876cf091464 37 //Fill the control's content and padding area
neilt6 0:b876cf091464 38 canvas->fillRect(contentPosX() - m_Padding, contentPosY() - m_Padding , contentWidth() + m_Padding * 2, contentHeight() + m_Padding * 2, m_BgColor);
neilt6 0:b876cf091464 39
neilt6 0:b876cf091464 40 //Check if we need to draw a border
neilt6 0:b876cf091464 41 if (m_Border > 0) {
neilt6 0:b876cf091464 42 //Draw the border
neilt6 0:b876cf091464 43 for (int i = 0; i < border(); i++) {
neilt6 0:b876cf091464 44 canvas->drawRect(m_X + m_Margin + i, m_Y + m_Margin + i, m_Width - 2 * (m_Margin + i), m_Height - 2 * (m_Margin + i), m_FgColor);
neilt6 0:b876cf091464 45 }
neilt6 0:b876cf091464 46 }
neilt6 0:b876cf091464 47
neilt6 0:b876cf091464 48 //We're no longer invalid
neilt6 0:b876cf091464 49 m_Invalid = false;
neilt6 0:b876cf091464 50 }
neilt6 0:b876cf091464 51
neilt6 1:f7003ec66a51 52 int Control::posX()
neilt6 0:b876cf091464 53 {
neilt6 0:b876cf091464 54 return m_X;
neilt6 0:b876cf091464 55 }
neilt6 0:b876cf091464 56
neilt6 0:b876cf091464 57 void Control::posX(int x)
neilt6 0:b876cf091464 58 {
neilt6 0:b876cf091464 59 //Set the new value
neilt6 0:b876cf091464 60 m_X = x;
neilt6 0:b876cf091464 61
neilt6 0:b876cf091464 62 //Force a repaint
neilt6 0:b876cf091464 63 m_Invalid = true;
neilt6 0:b876cf091464 64 }
neilt6 0:b876cf091464 65
neilt6 1:f7003ec66a51 66 int Control::posY()
neilt6 0:b876cf091464 67 {
neilt6 0:b876cf091464 68 return m_Y;
neilt6 0:b876cf091464 69 }
neilt6 0:b876cf091464 70
neilt6 0:b876cf091464 71 void Control::posY(int y)
neilt6 0:b876cf091464 72 {
neilt6 0:b876cf091464 73 //Set the new value
neilt6 0:b876cf091464 74 m_Y = y;
neilt6 0:b876cf091464 75
neilt6 0:b876cf091464 76 //Force a repaint
neilt6 0:b876cf091464 77 m_Invalid = true;
neilt6 0:b876cf091464 78 }
neilt6 0:b876cf091464 79
neilt6 1:f7003ec66a51 80 int Control::width()
neilt6 0:b876cf091464 81 {
neilt6 0:b876cf091464 82 return m_Width;
neilt6 0:b876cf091464 83 }
neilt6 0:b876cf091464 84
neilt6 0:b876cf091464 85 void Control::width(int w)
neilt6 0:b876cf091464 86 {
neilt6 0:b876cf091464 87 //Set the new value
neilt6 0:b876cf091464 88 m_Width = w;
neilt6 0:b876cf091464 89
neilt6 0:b876cf091464 90 //Force a repaint
neilt6 0:b876cf091464 91 m_Invalid = true;
neilt6 0:b876cf091464 92 }
neilt6 0:b876cf091464 93
neilt6 1:f7003ec66a51 94 int Control::height()
neilt6 0:b876cf091464 95 {
neilt6 0:b876cf091464 96 return m_Height;
neilt6 0:b876cf091464 97 }
neilt6 0:b876cf091464 98
neilt6 0:b876cf091464 99 void Control::height(int h)
neilt6 0:b876cf091464 100 {
neilt6 0:b876cf091464 101 //Set the new value
neilt6 0:b876cf091464 102 m_Height = h;
neilt6 0:b876cf091464 103
neilt6 0:b876cf091464 104 //Force a repaint
neilt6 0:b876cf091464 105 m_Invalid = true;
neilt6 0:b876cf091464 106 }
neilt6 0:b876cf091464 107
neilt6 1:f7003ec66a51 108 int Control::margin()
neilt6 0:b876cf091464 109 {
neilt6 0:b876cf091464 110 return m_Margin;
neilt6 0:b876cf091464 111 }
neilt6 0:b876cf091464 112
neilt6 0:b876cf091464 113 void Control::margin(int m)
neilt6 0:b876cf091464 114 {
neilt6 0:b876cf091464 115 //Update the value
neilt6 0:b876cf091464 116 m_Margin = m;
neilt6 0:b876cf091464 117
neilt6 0:b876cf091464 118 //Need to repaint
neilt6 0:b876cf091464 119 m_Invalid = true;
neilt6 0:b876cf091464 120 }
neilt6 0:b876cf091464 121
neilt6 1:f7003ec66a51 122 int Control::border()
neilt6 0:b876cf091464 123 {
neilt6 0:b876cf091464 124 return m_Border;
neilt6 0:b876cf091464 125 }
neilt6 0:b876cf091464 126
neilt6 0:b876cf091464 127 void Control::border(int b)
neilt6 0:b876cf091464 128 {
neilt6 0:b876cf091464 129 //Update the value
neilt6 0:b876cf091464 130 m_Border = b;
neilt6 0:b876cf091464 131
neilt6 0:b876cf091464 132 //Need to repaint
neilt6 0:b876cf091464 133 m_Invalid = true;
neilt6 0:b876cf091464 134 }
neilt6 0:b876cf091464 135
neilt6 1:f7003ec66a51 136 int Control::padding()
neilt6 0:b876cf091464 137 {
neilt6 0:b876cf091464 138 return m_Padding;
neilt6 0:b876cf091464 139 }
neilt6 0:b876cf091464 140
neilt6 0:b876cf091464 141 void Control::padding(int p)
neilt6 0:b876cf091464 142 {
neilt6 0:b876cf091464 143 //Update the value
neilt6 0:b876cf091464 144 m_Padding = p;
neilt6 0:b876cf091464 145
neilt6 0:b876cf091464 146 //Need to repaint
neilt6 0:b876cf091464 147 m_Invalid = true;
neilt6 0:b876cf091464 148 }
neilt6 0:b876cf091464 149
neilt6 1:f7003ec66a51 150 int Control::contentPosX()
neilt6 0:b876cf091464 151 {
neilt6 0:b876cf091464 152 return m_X + m_Margin + m_Border + m_Padding;
neilt6 0:b876cf091464 153 }
neilt6 0:b876cf091464 154
neilt6 1:f7003ec66a51 155 int Control::contentPosY()
neilt6 0:b876cf091464 156 {
neilt6 0:b876cf091464 157 return m_Y + m_Margin + m_Border + m_Padding;
neilt6 0:b876cf091464 158 }
neilt6 0:b876cf091464 159
neilt6 1:f7003ec66a51 160 int Control::contentWidth()
neilt6 0:b876cf091464 161 {
neilt6 0:b876cf091464 162 return m_Width - (m_Margin + m_Border + m_Padding) * 2;
neilt6 0:b876cf091464 163 }
neilt6 0:b876cf091464 164
neilt6 1:f7003ec66a51 165 int Control::contentHeight()
neilt6 0:b876cf091464 166 {
neilt6 0:b876cf091464 167 return m_Height - (m_Margin + m_Border + m_Padding) * 2;
neilt6 0:b876cf091464 168 }
neilt6 0:b876cf091464 169
neilt6 1:f7003ec66a51 170 unsigned int Control::foreColor()
neilt6 0:b876cf091464 171 {
neilt6 0:b876cf091464 172 return m_FgColor;
neilt6 0:b876cf091464 173 }
neilt6 0:b876cf091464 174
neilt6 0:b876cf091464 175 void Control::foreColor(unsigned int c)
neilt6 0:b876cf091464 176 {
neilt6 0:b876cf091464 177 //Update the value
neilt6 0:b876cf091464 178 m_FgColor = c;
neilt6 0:b876cf091464 179
neilt6 0:b876cf091464 180 //Need to repaint
neilt6 0:b876cf091464 181 m_Invalid = true;
neilt6 0:b876cf091464 182 }
neilt6 0:b876cf091464 183
neilt6 1:f7003ec66a51 184 unsigned int Control::backColor()
neilt6 0:b876cf091464 185 {
neilt6 0:b876cf091464 186 return m_BgColor;
neilt6 0:b876cf091464 187 }
neilt6 0:b876cf091464 188
neilt6 0:b876cf091464 189 void Control::backColor(unsigned int c)
neilt6 0:b876cf091464 190 {
neilt6 0:b876cf091464 191 //Update the value
neilt6 0:b876cf091464 192 m_BgColor = c;
neilt6 0:b876cf091464 193
neilt6 0:b876cf091464 194 //Need to repaint
neilt6 0:b876cf091464 195 m_Invalid = true;
neilt6 0:b876cf091464 196 }
neilt6 0:b876cf091464 197
neilt6 1:f7003ec66a51 198 const char* Control::text()
neilt6 0:b876cf091464 199 {
neilt6 0:b876cf091464 200 return m_Text;
neilt6 0:b876cf091464 201 }
neilt6 0:b876cf091464 202
neilt6 1:f7003ec66a51 203 void Control::text(const char* text)
neilt6 0:b876cf091464 204 {
neilt6 0:b876cf091464 205 //Update the value
neilt6 0:b876cf091464 206 m_Text = text;
neilt6 0:b876cf091464 207
neilt6 0:b876cf091464 208 //Need to repaint
neilt6 0:b876cf091464 209 m_Invalid = true;
neilt6 0:b876cf091464 210 }
neilt6 0:b876cf091464 211
neilt6 1:f7003ec66a51 212 Font* Control::font()
neilt6 0:b876cf091464 213 {
neilt6 0:b876cf091464 214 return m_Font;
neilt6 0:b876cf091464 215 }
neilt6 0:b876cf091464 216
neilt6 1:f7003ec66a51 217 void Control::font(Font* fnt)
neilt6 0:b876cf091464 218 {
neilt6 0:b876cf091464 219 //Update the value
neilt6 0:b876cf091464 220 m_Font = fnt;
neilt6 0:b876cf091464 221
neilt6 0:b876cf091464 222 //Need to repaint
neilt6 0:b876cf091464 223 m_Invalid = true;
neilt6 0:b876cf091464 224 }
neilt6 0:b876cf091464 225
neilt6 1:f7003ec66a51 226 bool Control::invalid()
neilt6 0:b876cf091464 227 {
neilt6 0:b876cf091464 228 return m_Invalid;
neilt6 0:b876cf091464 229 }
neilt6 0:b876cf091464 230
neilt6 1:f7003ec66a51 231 void Control::invalidate()
neilt6 0:b876cf091464 232 {
neilt6 0:b876cf091464 233 m_Invalid = true;
neilt6 0:b876cf091464 234 }