1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
/*
Simple GUI & Network demo application,
Copyright (c) 2000 by Magnus Norddahl and Kenneth Gangstoe.
*/
#include "canvas_view.h"
#include "sketch.h"
#include <ClanLib/display.h>
#include <ClanLib/gui.h>
/////////////////////////////////////////////////////////////////////////////
// Construction:
CanvasView::CanvasView(MainFrame *parent)
: View(parent)
{
current_sketch = NULL;
drawing = false;
int xlen = get_width();
int ylen = get_height();
component_sketch = new CL_Component(CL_Rect(0, 0, xlen, ylen - 20), this);
button_close = new CL_Button(
CL_Rect(0, ylen - 20, 70, ylen), "Close", this);
button_new_picture = new CL_Button(
CL_Rect(90, ylen - 20, 160, ylen), "Add picture", this);
button_new_sketch = new CL_Button(
CL_Rect(160, ylen - 20, 230, ylen), "Add sketch", this);
button_invite = new CL_Button(
CL_Rect(250, ylen - 20, 320, ylen), "Invite", this);
button_kick = new CL_Button(
CL_Rect(320, ylen - 20, 390, ylen), "Kick", this);
button_undo = new CL_Button(
CL_Rect(410, ylen - 20, 480, ylen), "Undo", this);
button_redo = new CL_Button(
CL_Rect(480, ylen - 20, 550, ylen), "Redo", this);
check_undo_redo();
/* layout.add_resize_position(
component_sketch, CL_LayoutManager::y1, this, CL_LayoutManager::y2);
layout.add_resize_position(
component_sketch, CL_LayoutManager::y2, this, CL_LayoutManager::y2);
layout.add_resize_position(
component_sketch, CL_LayoutManager::x2, this, CL_LayoutManager::x2);
layout.add_resize_position(
button_close, CL_LayoutManager::y1, this, CL_LayoutManager::y2);
layout.add_resize_position(
button_close, CL_LayoutManager::y2, this, CL_LayoutManager::y2);
layout.add_resize_position(
button_new_sketch, CL_LayoutManager::x1, this, CL_LayoutManager::x2);
layout.add_resize_position(
button_new_sketch, CL_LayoutManager::x2, this, CL_LayoutManager::x2);
layout.add_resize_position(
button_new_sketch, CL_LayoutManager::y1, this, CL_LayoutManager::y2);
layout.add_resize_position(
button_new_sketch, CL_LayoutManager::y2, this, CL_LayoutManager::y2);
*/
// Connect events to signals:
slots.connect(sig_paint(), this, &CanvasView::on_paint);
slots.connect(component_sketch->sig_paint(), this, &CanvasView::on_paint_sketch);
slots.connect(button_new_picture->sig_clicked(), this, &CanvasView::on_button_new_picture);
slots.connect(button_new_sketch->sig_clicked(), this, &CanvasView::on_button_new_sketch);
slots.connect(button_kick->sig_clicked(), this, &CanvasView::on_button_kick);
slots.connect(button_invite->sig_clicked(), this, &CanvasView::on_button_invite);
slots.connect(button_undo->sig_clicked(), this, &CanvasView::on_button_undo);
slots.connect(button_redo->sig_clicked(), this, &CanvasView::on_button_redo);
slots.connect(button_close->sig_clicked(), (CL_Component*)this, &CL_Component::quit);
slots.connect(sig_mouse_down(), this, &CanvasView::on_mouse_down);
slots.connect(sig_mouse_up(), this, &CanvasView::on_mouse_up);
slots.connect(sig_mouse_move(), this, &CanvasView::on_mouse_move);
set_title("Canvas");
}
CanvasView::~CanvasView()
{
}
/////////////////////////////////////////////////////////////////////////////
// Attributes:
/////////////////////////////////////////////////////////////////////////////
// Operations:
#define BUTTON_WIDTH 100
#define BUTTON_HEIGHT 20
void CanvasView::add_sketch()
{
current_sketch = new Sketch;
canvas.add_sketch(current_sketch);
int count = button_sketches.size();
// Calculate position and create new button
int xpos = get_width();
int ypos = count * BUTTON_HEIGHT;
CL_Button *button = new CL_Button(
CL_Rect(xpos - BUTTON_WIDTH, ypos, xpos, ypos + BUTTON_HEIGHT), "Sketch", this);
button_sketches.push_back(button);
CL_Slot slot = button->sig_clicked().connect(this, &CanvasView::on_button_sketch, current_sketch);
slots_button_sketch.push_back(slot);
layout.add_resize_position(
button, CL_LayoutManager::x1, this, CL_LayoutManager::x2);
layout.add_resize_position(
button, CL_LayoutManager::x2, this, CL_LayoutManager::x2);
}
/////////////////////////////////////////////////////////////////////////////
// Events:
void CanvasView::on_button_new_sketch()
{
add_sketch();
check_undo_redo();
}
void CanvasView::on_button_new_picture()
{
std::string filename = CL_FileDialog::open(this);
if(filename.length() == 0)
return;
CL_Surface *surface;
try
{
surface = new CL_Surface(filename);
}
catch(CL_Error err)
{
CL_MessageBox::info(err.message, this);
return;
}
current_picture = surface;
canvas.add_picture(surface);
int count = button_sketches.size();
// Calculate position and create new button
int xpos = get_width();
int ypos = count * BUTTON_HEIGHT;
CL_Button *button = new CL_Button(
CL_Rect(xpos - BUTTON_WIDTH, ypos, xpos, ypos + BUTTON_HEIGHT), "Picture", this);
button_sketches.push_back(button);
CL_Slot slot = button->sig_clicked().connect(this, &CanvasView::on_button_picture, current_picture);
slots_button_picture.push_back(slot);
layout.add_resize_position(
button, CL_LayoutManager::x1, this, CL_LayoutManager::x2);
layout.add_resize_position(
button, CL_LayoutManager::x2, this, CL_LayoutManager::x2);
}
void CanvasView::on_button_sketch(Sketch *sketch)
{
if(sketch->is_visible())
sketch->show(false);
else
sketch->show(true);
current_sketch = sketch;
check_undo_redo();
}
void CanvasView::on_button_picture(CL_Surface *picture)
{
/* if(picture->is_visible())
picture->show(false);
else
picture->show(true);
*/
current_picture = picture;
}
void CanvasView::on_button_undo()
{
if(current_sketch)
current_sketch->undo();
check_undo_redo();
}
void CanvasView::on_button_redo()
{
if(current_sketch)
current_sketch->redo();
check_undo_redo();
}
void CanvasView::on_mouse_down(const CL_Key &key)
{
if(current_sketch)
drawing = true;
}
void CanvasView::on_mouse_up(const CL_Key &key)
{
drawing = false;
}
void CanvasView::on_mouse_move(CL_Component *component, int x, int y)
{
static int old_x = -1;
static int old_y = -1;
if(drawing)
{
current_sketch->add_line(CL_Rect(old_x, old_y, x, y));
check_undo_redo();
}
old_x = x;
old_y = y;
}
void CanvasView::on_paint()
{
// Background
CL_Display::fill_rect(0, 0, get_width(), get_height() - 20, 1.0f, 1.0f, 1.0f);
// Lower button-bar
CL_Display::fill_rect(0, get_height() - 20, get_width(), get_height(), 141/255.0f, 161/255.0f, 189/255.0f);
}
void CanvasView::on_paint_sketch()
{
canvas.draw();
}
void CanvasView::check_undo_redo()
{
bool undo = false, redo = false;
if(current_sketch)
{
if(current_sketch->has_undo())
undo = true;
if(current_sketch->has_redo())
redo = true;
}
button_undo->enable(undo);
button_redo->enable(redo);
}
void CanvasView::on_button_invite()
{
CL_Rect rect = button_invite->get_position();
int xpos = rect.x1;
int ypos = rect.y1 - 63;
/* CL_PopupMenu *popup = new CL_PopupMenu(CL_Point(xpos, ypos), this);
CL_Button *button1 = new CL_Button("Everyone", popup);
CL_Button *button2 = new CL_Button("Sphair", popup);
CL_Button *button3 = new CL_Button("Mbn", popup);
popup->set_focus();
*/
}
void CanvasView::on_button_kick()
{
}
|