Checkpoints.
[hackover2013-badge-firmware.git] / mock / mock / lcd-window.cc
1 #include "lcd-window.hh"
2 #include "badge_worker.hh"
3 #include "mock/event.h"
4
5 #include <gtkmm/main.h>
6
7 namespace badge2013 {
8 lcd_drawingarea::lcd_drawingarea(BaseObjectType *cobject,
9 Glib::RefPtr<Gtk::Builder> const &)
10 : Gtk::DrawingArea(cobject)
11 {
12 sig_redraw_.connect(sigc::mem_fun(*this, &lcd_drawingarea::force_redraw));
13 badge_framebuffer_clear(&framebuffer_);
14 }
15
16 lcd_drawingarea::~lcd_drawingarea() { }
17
18 void lcd_drawingarea::push_framebuffer(badge_framebuffer const &fb) {
19 framebuffer_ = fb;
20 sig_redraw_();
21 }
22
23 void lcd_drawingarea::force_redraw() {
24 Glib::RefPtr<Gdk::Window> win = get_window();
25 if(win) {
26 win->invalidate(true);
27 }
28 }
29
30 bool lcd_drawingarea::on_draw(Cairo::RefPtr<Cairo::Context> const &cr) {
31 Gtk::Allocation alloc = get_allocation();
32 double w_width = alloc.get_width ();
33 double w_height = alloc.get_height();
34
35 double c_width = w_width / BADGE_DISPLAY_WIDTH;
36 double c_height = w_height / BADGE_DISPLAY_HEIGHT;
37
38 cr->save();
39
40 for(int i = 0; i < BADGE_DISPLAY_WIDTH; ++i) {
41 for(int j = 0; j < BADGE_DISPLAY_HEIGHT; ++j) {
42 if(badge_framebuffer_pixel(&framebuffer_, i, j)) {
43 cr->rectangle(c_width * i, c_height * j,
44 c_width , c_height);
45 }
46 }
47 }
48
49 cr->fill();
50 cr->restore();
51
52 return true;
53 }
54
55 ///////////////////////////////////////////////////////
56 ///////////////////////////////////////////////////////
57 ///////////////////////////////////////////////////////
58
59 lcd_window::lcd_window(BaseObjectType *cobject, Glib::RefPtr<Gtk::Builder> const &glade)
60 : Gtk::Window(cobject),
61 worker_(this)
62 {
63 glade->get_widget_derived("canvas", canvas_);
64
65 Glib::signal_timeout().connect(sigc::mem_fun(*this, &lcd_window::on_game_tick ), 11);
66 sig_close_.connect(sigc::mem_fun(*this, &lcd_window::hide));
67 }
68
69 lcd_window::~lcd_window() { }
70
71 void lcd_window::push_framebuffer(badge_framebuffer const &fb) {
72 canvas_->push_framebuffer(fb);
73 }
74
75 void lcd_window::request_close() {
76 sig_close_();
77 }
78
79 bool lcd_window::on_key_press_event (GdkEventKey* event) {
80 uint8_t new_state = key_state_;
81
82 switch(event->keyval) {
83 case GDK_KEY_e: new_state |= BADGE_EVENT_KEY_UP; break;
84 case GDK_KEY_s: new_state |= BADGE_EVENT_KEY_LEFT; break;
85 case GDK_KEY_d: new_state |= BADGE_EVENT_KEY_DOWN; break;
86 case GDK_KEY_f: new_state |= BADGE_EVENT_KEY_RIGHT; break;
87 case GDK_KEY_a: new_state |= BADGE_EVENT_KEY_CENTER; break;
88 case GDK_KEY_j: new_state |= BADGE_EVENT_KEY_BTN_A; break;
89 case GDK_KEY_k: new_state |= BADGE_EVENT_KEY_BTN_B; break;
90 }
91
92 badge_event_push(badge_event_new(BADGE_EVENT_USER_INPUT, key_state_, new_state));
93 key_state_ = new_state;
94
95 return true;
96 }
97
98 bool lcd_window::on_key_release_event(GdkEventKey* event) {
99 uint8_t new_state = key_state_;
100
101 switch(event->keyval) {
102 case GDK_KEY_e: new_state &= ~BADGE_EVENT_KEY_UP; break;
103 case GDK_KEY_s: new_state &= ~BADGE_EVENT_KEY_LEFT; break;
104 case GDK_KEY_d: new_state &= ~BADGE_EVENT_KEY_DOWN; break;
105 case GDK_KEY_f: new_state &= ~BADGE_EVENT_KEY_RIGHT; break;
106 case GDK_KEY_a: new_state &= ~BADGE_EVENT_KEY_CENTER; break;
107 case GDK_KEY_j: new_state &= ~BADGE_EVENT_KEY_BTN_A; break;
108 case GDK_KEY_k: new_state &= ~BADGE_EVENT_KEY_BTN_B; break;
109 }
110
111 badge_event_push(badge_event_new(BADGE_EVENT_USER_INPUT, key_state_, new_state));
112 key_state_ = new_state;
113
114 return true;
115 }
116
117 bool lcd_window::on_game_tick() {
118 badge_event_push(badge_event_new(BADGE_EVENT_GAME_TICK, key_state_, key_state_));
119
120 return true;
121 }
122
123 void lcd_window::on_show() {
124 Gtk::Window::on_show();
125
126 if(!running_) {
127 running_ = true;
128 Glib::Thread::create(sigc::mem_fun(worker_, &badge_worker::run));
129 }
130 }
131 }
This page took 0.054424 seconds and 5 git commands to generate.