Sciter: F:/hsmile5/sdk/include/behaviors/behavior_drawing.cpp Source File

Sciter  3.3.2.5
Sciter API
behavior_drawing.cpp
Go to the documentation of this file.
1 #include "stdafx.h"
2 #include "sciter-x.h"
3 #include "sciter-x-behavior.h"
4 #include "sciter-x-graphics.hpp"
5 #include <time.h>
6 
7 namespace sciter
8 {
9 /*
10 BEHAVIOR: native-clock
11  - draws content layer using sciter-x-graphics.hpp primitives.
12 
13 SAMPLE:
14  See: samples/behaviors/native-drawing.htm
15 */
16 
17  struct native_clock: public event_handler
18 {
19  // ctor
20   native_clock() {}
21   virtual ~native_clock() {}
22 
23   virtual bool subscription( HELEMENT he, UINT& event_groups )
24  {
25  event_groups = HANDLE_DRAW // it does drawing
26  | HANDLE_TIMER; // and handles timer
27  return true;
28  }
29 
30   virtual void attached (HELEMENT he )
31  {
32  dom::element(he).start_timer(1000);
33  }
34   virtual void detached (HELEMENT he ) { delete this; }
35 
36 
37   virtual bool handle_timer (HELEMENT he,TIMER_PARAMS& params )
38  {
39  dom::element(he).refresh(); // refresh element's area
40  return true; // keep ticking
41  }
42 
43   virtual bool handle_draw (HELEMENT he, DRAW_PARAMS& params )
44  {
45  if( params.cmd != DRAW_CONTENT ) return false; // drawing only content layer
46 
47  const float PI = 3.141592653f;
48 
49  float w = float(params.area.right - params.area.left);
50  float h = float(params.area.bottom - params.area.top);
51 
52  float scale = w < h? w / 300.0f: h / 300.0f;
53 
54  time_t rawtime;
55  time (&rawtime);
56  struct tm timeinfo = *localtime (&rawtime);
57 
58  sciter::graphics gfx(params.gfx);
59  gfx.state_save();
60 
61  gfx.translate(params.area.left + w / 2.0f,params.area.top + h / 2.0f);
62  gfx.scale(scale,scale);
63  gfx.rotate(-PI/2);
64  gfx.line_color(0);
65  gfx.line_width(8.f);
66  gfx.line_cap(SCITER_LINE_CAP_ROUND);
67 
68  // Hour marks
69  gfx.state_save();
70  gfx.line_color(gcolor(0x32,0x5F,0xA2));
71  for (int i = 0; i < 12; ++i) {
72  gfx.rotate(PI/6);
73  gfx.line(137.f,0,144.f,0);
74  }
75  gfx.state_restore();
76 
77  // Minute marks
78  gfx.state_save();
79  gfx.line_width(3.f);
80  gfx.line_color(gcolor(0xA5,0x2A, 0x2A));
81  for (int i = 0; i < 60; ++i) {
82  if ( i % 5 != 0)
83  gfx.line(143,0,146,0);
84  gfx.rotate(PI/30.f);
85  }
86  gfx.state_restore();
87 
88  int sec = timeinfo.tm_sec;
89  int min = timeinfo.tm_min;
90  int hr = timeinfo.tm_hour;
91  hr = hr >= 12 ? hr - 12 : hr;
92 
93  // draw Hours
94  gfx.state_save();
95  gfx.rotate( hr*(PI/6) + (PI/360)*min + (PI/21600)*sec );
96  gfx.line_width(14);
97  gfx.line_color(gcolor(0x32,0x5F,0xA2));
98  gfx.line(-20,0,70,0);
99  gfx.state_restore();
100 
101  // draw Minutes
102  gfx.state_save();
103  gfx.rotate( (PI/30)*min + (PI/1800)*sec );
104  gfx.line_width(10);
105  gfx.line_color(gcolor(0x32,0x5F,0xA2));
106  gfx.line(-28,0,100,0);
107  gfx.state_restore();
108 
109  // draw seconds
110  gfx.state_save();
111  gfx.rotate(sec * PI/30);
112  gfx.line_color(gcolor(0xD4,0,0));
113  gfx.fill_color(gcolor(0xD4,0,0));
114  gfx.line_width(6);
115  gfx.line(-30,0,83,0);
116  gfx.ellipse(0,0,10,10);
117 
118  gfx.fill_color(0);
119  gfx.ellipse(95,0,10,10);
120  gfx.state_restore();
121 
122  gfx.state_restore();
123 
124  return false;
125 
126  }
127 
128 };
129 
130  struct native_clock_factory: public behavior_factory {
131 
132   native_clock_factory(): behavior_factory("native-clock") { }
133 
134  // the only behavior_factory method:
135   virtual event_handler* create(HELEMENT he) {
136  return new native_clock();
137  }
138 
139 };
140 
141 // instantiating and attaching it to the global list
142  native_clock_factory native_clock_factory_instance;
143 
144 
145 }
Behaiviors support (a.k.a windowless scriptable controls)
virtual bool handle_draw(HELEMENT he, DRAW_PARAMS &params)
void start_timer(unsigned int ms, void *timer_id=0)
virtual event_handler * create(HELEMENT he)
native_clock_factory native_clock_factory_instance
virtual bool subscription(HELEMENT he, UINT &event_groups)
void * HELEMENT
Definition: sciter-x-dom.h:40
virtual bool handle_timer(HELEMENT he, TIMER_PARAMS &params)
virtual void detached(HELEMENT he)
void refresh(RECT rc) const
virtual void attached(HELEMENT he)

AltStyle によって変換されたページ (->オリジナル) /