Debian Codegraph @

lib/ninix/dll/hanayu.rb

raw
1 # -*- coding: utf-8 -*-
2 #
3 # hanayu.rb - a "花柚" compatible Saori module for ninix
4 # Copyright (C) 2002-2019 by Shyouzou Sugitani <[email protected]>
5 # Copyright (C) 2002, 2003 by MATSUMURA Namihiko <[email protected]>
6 #
7 # This program is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License (version 2) as
9 # published by the Free Software Foundation. It is distributed in the
10 # hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
11 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 # PURPOSE. See the GNU General Public License for more details.
13 #
14
15 # TODO:
16 # - usetime, line, radar 以外の形式のグラフへの対応.
17
18
19 require "gtk4"
20
21 require_relative "../pix"
22 require_relative "../dll"
23 require_relative "../logging"
24
25
26 module Hanayu
27
28 class Saori < DLL::SAORI
29
30 DBNAME = 'HANAYU.db'
31
32 def initialize
33 super()
34 @graphs = {}
35 @data = {}
36 end
37
38 def setup
39 @dbpath = File.join(@dir, DBNAME)
40 @graphs = {}
41 @data = read_hanayu_txt(@dir)
42 unless @data.nil? or @data.empty?
43 read_db()
44 return 1
45 else
46 return 0
47 end
48 end
49
50 def read_hanayu_txt(dir)
51 graphs = {}
52 begin
53 open(File.join(dir, 'hanayu.txt'), 'r', :encoding => 'CP932') do |f|
54 data = {}
55 name = ''
56 tmp_name = ''
57 for line in f
58 line = line.encode('UTF-8', :invalid => :replace, :undef => :replace).strip()
59 next if line.empty?
60 next if line.start_with?('//')
61 if line.start_with?('[') # bln.txt like format
62 graphs[name] = data
63 data = {}
64 end_ = line.find(']')
65 if end_ < 0
66 end_ = line.length
67 name = line[1..end_-1]
68 end
69 elsif line == '{' # surfaces.txt like format
70 graphs[name] = data
71 data = {}
72 name = tmp_name ## FIXME
73 elsif line == '}' # surfaces.txt like format
74 graphs[name] = data
75 data = {}
76 name = ''
77 elsif line.include?(',')
78 key, value = line.split(',', 2)
79 key.strip!
80 value.strip!
81 data[key] = value
82 elsif not name
83 tmp_name = line
84 end
85 end
86 graphs[name] = data unless data.empty?
87 return graphs
88 end
89 rescue
90 return nil
91 end
92 end
93
94 def finalize
95 @graphs.each_key {|name| @graphs[name].destroy() }
96 @graphs = {}
97 @data = {}
98 write_db()
99 return 1
100 end
101
102 def time_to_key(base, offset)
103 year = base.year
104 month = base.month
105 day = base.day
106 target_time = (Time.new(year, month, day, 0, 0, 0) + offset * 24 *60 * 60)
107 year = target_time.year
108 month = target_time.month
109 day = target_time.day
110 key = (year * 10000 + month * 100 + day).to_s
111 return key, year, month, day
112 end
113
114 def read_db
115 @seven_days = []
116 current_time = @last_update = Time.now
117 for index_ in -6..0
118 key, year, month, day = time_to_key(current_time, index_)
119 @seven_days << [key, year, month, day, 0.0]
120 end
121 begin
122 open(@dbpath) do |f|
123 ver = nil
124 for line in f
125 line = line.strip()
126 next if line.empty?
127 if ver.nil?
128 if line == '# Format: v1.0'
129 ver = 1
130 end
131 next
132 end
133 next unless line.include?(',')
134 key, value = line.split(',', 2)
135 for index_ in 0..6
136 if @seven_days[index_][0] == key
137 @seven_days[index_][4] = Float(value)
138 end
139 end
140 end
141 end
142 rescue
143 return
144 end
145 end
146
147 def update_db
148 current_time = Time.now
149 old_seven_days = []
150 old_seven_days.concat(@seven_days)
151 @seven_days = []
152 for index_ in -6..0
153 key, year, month, day = time_to_key(current_time, index_)
154 @seven_days << [key, year, month, day, 0.0]
155 end
156 for i in 0..6
157 key = old_seven_days[i][0]
158 for j in 0..6
159 if @seven_days[j][0] == key
160 @seven_days[j][4] = old_seven_days[i][4]
161 if j == 6
162 @seven_days[j][4] = (@seven_days[j][4] + \
163 (current_time - \
164 @last_update) / \
165 (60.0 * 60.0))
166 end
167 end
168 end
169 end
170 @last_update = current_time
171 end
172
173 def write_db
174 update_db()
175 begin
176 open(@dbpath, 'w') do |f|
177 f.write("# Format: v1.0\n")
178 for index_ in 0..6
179 f.write(@seven_days[index_][0].to_s + ", " + @seven_days[index_][4].to_s + "\n")
180 end
181 end
182 rescue # IOError, SystemCallError
183 Logging::Logging.error('HANAYU: cannot write database (ignored)')
184 end
185 end
186
187 def execute(argument)
188 return RESPONSE[400] if argument.nil? or argument.empty?
189 command = argument[0]
190 case command
191 when 'show'
192 if argument.length >= 2
193 name = argument[1]
194 if @graphs.include?(name)
195 @graphs[name].destroy()
196 end
197 else
198 name = ''
199 end
200 return RESPONSE[400] unless @data.include?(name)
201 if @data[name].include?('graph') and \
202 ['line', 'bar', 'radar', 'radar2'].include?(@data[name]['graph'])
203 graph_type = @data[name]['graph']
204 else
205 graph_type = 'usetime'
206 end
207 case graph_type
208 when 'usetime'
209 update_db()
210 new_args = []
211 for index_ in 0..6
212 date = [@seven_days[index_][2].to_s,
213 '/',
214 @seven_days[index_][3].to_s].join("")
215 new_args << date
216 hours = @seven_days[index_][4]
217 new_args << hours
218 end
219 @graphs[name] = Line.new(
220 @dir, @data[name], :args => new_args, :limit_min => 0, :limit_max => 24)
221 when 'line'
222 @graphs[name] = Line.new(
223 @dir, @data[name], :args => argument[2..-1])
224 when 'bar'
225 @graphs[name] = Bar.new(
226 @dir, @data[name], :args => argument[2..-1])
227 when 'radar'
228 @graphs[name] = Radar.new(
229 @dir, @data[name], :args => argument[2..-1])
230 when 'radar2'
231 @graphs[name] = Radar2.new(
232 @dir, @data[name], :args => argument[2..-1])
233 end
234 when 'hide'
235 if argument.length >= 2
236 name = argument[1]
237 else
238 name = ''
239 end
240 if @graphs.include?(name)
241 @graphs[name].destroy()
242 else
243 return RESPONSE[400]
244 end
245 else
246 return RESPONSE[400]
247 end
248 return RESPONSE[204]
249 end
250 end
251
252
253 class Graph
254
255 WIDTH = 450
256 HEIGHT = 340
257
258 def initialize(dir, data, args: [], limit_min: nil, limit_max: nil)
259 @dir = dir
260 @data = data
261 @args = args
262 @min = limit_min
263 @max = limit_max
264 create_window()
265 @window.show()
266 end
267
268 def create_window
269 @window = Gtk::Window.new
270 @window.set_title('花柚') # UTF-8
271 @window.set_decorated(false)
272 @window.set_resizable(false)
273 @window.signal_connect('delete_event') do |w ,e|
274 next delete(w, e)
275 end
276 scrn = Gdk::Screen.default
277 left, top = 0, 0 # XXX
278 scrn_w = scrn.width - left
279 scrn_h = scrn.height - top
280 @x = (left + (scrn_w / 2).to_i)
281 @y = (top + (scrn_h / 4).to_i)
282 @window.move(@x, @y)
283 @darea = Gtk::DrawingArea.new
284 @darea.set_events(Gdk::EventMask::EXPOSURE_MASK|
285 Gdk::EventMask::BUTTON_PRESS_MASK)
286 @darea.signal_connect('draw') do |w ,e|
287 redraw(w, e)
288 next true
289 end
290 @darea.signal_connect('button_press_event') do |w ,e |
291 next button_press(w, e)
292 end
293 @darea.set_size_request(WIDTH, HEIGHT)
294 @darea.show()
295 @window.add(@darea)
296 @darea.realize()
297 @layout = Pango::Layout.new(@darea.pango_context)
298 surface = nil
299 if @data.include?('background.filename')
300 path = File.join(
301 @dir, @data['background.filename'].gsub('\\', '/'))
302 begin
303 surface = Pix.create_surface_from_file(path, is_pnr=0)
304 rescue
305 surface = nil
306 end
307 @surface = surface
308 end
309 end
310
311 def get_color(target)
312 fail "assert" unless ['font', 'line', 'frame', 'bar', 'background'].include?(target)
313 if target == 'background'
314 r = g = b = 255 # white
315 else
316 r = g = b = 0 # black
317 end
318 name = [target, '.color'].join("")
319 if @data.include?(name)
320 r = @data[name][0..1].to_i(16)
321 g = @data[name][2..3].to_i(16)
322 b = @data[name][4..5].to_i(16)
323 else
324 name_r = [name, '.r'].join("")
325 if @data.include?(name_r)
326 r = @data[name_r].to_i
327 end
328 name_g = [name, '.g'].join("")
329 if @data.include?(name_g)
330 g = @data[name_g].to_i
331 end
332 name_b = [name, '.b'].join("")
333 if @data.include?(name_b)
334 b = @data[name_b].to_i
335 end
336 end
337 return [r / 255.0, g / 255.0, b / 255.0]
338 end
339
340 def draw_title(widget, cr)
341 return unless @data.has_key?('title')
342 @title = @data['title']
343 font_size = 12 # pixel
344 @font_desc = Pango::FontDescription.new
345 @font_desc.set_family('Sans')
346 @font_desc.set_size(font_size * Pango::SCALE)
347 cr.set_source_rgb(*get_color('font'))
348 w = widget.allocated_width
349 h = widget.allocated_height
350 cr.translate(w, 0)
351 cr.rotate(Math::PI / 2.0)
352 layout = Pango::Layout.new(widget.pango_context)
353 layout.set_font_description(@font_desc)
354 context = layout.context
355 default_gravity = context.base_gravity # XXX
356 context.base_gravity = Pango::Gravity::EAST # Vertical Text
357 layout.set_text(@title)
358 layout.set_wrap(:word)
359 tw, th = layout.pixel_size
360 cr.move_to(58, w - 20 - th)
361 cr.show_pango_layout(layout)
362 context.base_gravity = default_gravity # XXX
363 end
364
365 def draw_frame(widget, cr)
366 #pass
367 end
368
369 def draw_graph(widget, cr)
370 #pass
371 end
372
373 def redraw(widget, cr)
374 cr.save()
375 cr.set_source_rgb(*get_color('background'))
376 cr.paint()
377 unless @surface.nil?
378 width = @surface.width
379 height = @surface.height
380 xoffset = ((WIDTH - width) / 2).to_i
381 yoffset = ((HEIGHT - height) / 2).to_i
382 cr.set_source(@surface, xoffset, yoffset)
383 cr.set_operator(Cairo::OPERATOR_SOURCE)
384 cr.paint()
385 end
386 cr.restore()
387 cr.save()
388 draw_title(widget, cr)
389 cr.restore()
390 cr.save()
391 draw_frame(widget, cr)
392 cr.restore()
393 cr.save()
394 draw_graph(widget, cr)
395 cr.restore()
396 end
397
398 def button_press(window, event)
399 case event.event_type
400 when Gdk::EventType::BUTTON_PRESS
401 @window.begin_move_drag(
402 event.button, event.x_root.to_i, event.y_root.to_i,
403 event.time)
404 when Gdk::EventType::DOUBLE_BUTTON_PRESS # double click
405 destroy()
406 end
407 return true
408 end
409
410 def delete(window, event)
411 return true
412 end
413
414 def destroy
415 @window.destroy() unless @window.nil?
416 @window = nil
417 @timeout_id = nil
418 end
419 end
420
421
422 class Line < Graph
423
424 def draw_frame(widget, cr)
425 frame_width = 2
426 if @data.include?('frame.width')
427 frame_width = @data['frame.width'].to_i
428 end
429 cr.set_line_width(frame_width)
430 cr.set_source_rgb(*get_color('frame'))
431 cr.move_to(60, 48)
432 cr.line_to(60, 260)
433 cr.line_to(420, 260)
434 cr.stroke()
435 end
436
437 def draw_graph(widget, cr)
438 cr.set_source_rgb(*get_color('font'))
439 num = (@args.length / 2).to_i
440 step = (368 / num).to_i
441 for index_ in 0..num-1
442 @layout.set_text(@args[index_ * 2])
443 w, h = @layout.pixel_size
444 pos_x = (60 + index_ * step + (step / 2).to_i - (w / 2).to_i)
445 pos_y = 268
446 cr.move_to(pos_x, pos_y)
447 cr.show_pango_layout(@layout)
448 end
449 unless @min.nil?
450 limit_min = @min
451 else
452 limit_min = @args[1]
453 for index_ in 2..num-1
454 if @args[index_ * 2] < limit_min
455 limit_min = @args[index_ * 2]
456 end
457 end
458 end
459 unless @max.nil?
460 limit_max = @max
461 else
462 limit_max = @args[1]
463 for index_ in 2..num-1
464 if @args[index_ * 2] > limit_max
465 limit_max = @args[index_ * 2]
466 end
467 end
468 end
469 line_width = 2
470 if @data.include?('line.width')
471 line_width = @data['line.width'].to_i
472 end
473 cr.set_line_width(line_width)
474 cr.set_source_rgb(*get_color('line'))
475 for index_ in 1..num-1
476 src_x = (60 + (index_ - 1) * step + (step / 2).to_i)
477 src_y = (220 - (
478 168 * @args[(index_ - 1) * 2 + 1] / (limit_max - limit_min)).to_i)
479 dst_x = (60 + index_ * step + (step / 2).to_i)
480 dst_y = (220 - (168 * @args[index_ * 2 + 1] / (limit_max - limit_min)).to_i)
481 cr.move_to(src_x, src_y)
482 cr.line_to(dst_x, dst_y)
483 cr.stroke()
484 end
485 for index_ in 0..num-1
486 surface = nil
487 if @args[index_ * 2 + 1] == limit_min and \
488 @data.include?('mark0.filename')
489 path = File.join(
490 @dir, @data['mark0.filename'].gsub('\\', '/'))
491 if File.exist?(path)
492 begin
493 surface = Pix.create_surface_from_file(path)
494 rescue
495 surface = nil
496 end
497 end
498 elsif @args[index_ * 2 + 1] == limit_max and \
499 @data.include?('mark2.filename')
500 path = File.join(
501 @dir, @data['mark2.filename'].gsub('\\', '/'))
502 if File.exist?(path)
503 begin
504 surface = Pix.create_surface_from_file(path)
505 rescue
506 surface = nil
507 end
508 end
509 elsif @data.include?('mark1.filename')
510 path = File.join(
511 @dir, @data['mark1.filename'].gsub('\\', '/'))
512 if File.exist?(path)
513 begin
514 surface = Pix.create_surface_from_file(path)
515 rescue
516 surface = nil
517 end
518 end
519 end
520 unless surface.nil?
521 w = surface.width
522 h = surface.height
523 x = (60 + index_ * step + (step / 2).to_i - (w / 2).to_i)
524 y = (220 - (
525 168 * @args[index_ * 2 + 1] / (limit_max - limit_min)).to_i - (h / 2).to_i)
526 cr.set_source(surface, x, y)
527 cr.paint()
528 end
529 end
530 end
531 end
532
533
534 class Bar < Graph
535
536 def draw_frame(widget, cr)
537 frame_width = 2
538 if @data.include?('frame.width')
539 frame_width = @data['frame.width'].to_i
540 end
541 cr.set_line_width(frame_width)
542 cr.set_source_rgb(*get_color('frame'))
543 cr.move_to(60, 48)
544 cr.line_to(60, 260)
545 cr.line_to(420, 260)
546 cr.stroke()
547 end
548
549 def draw_graph(widget, cr) ## FIXME
550 cr.set_source_rgb(*get_color('bar'))
551 bar_with = 20 ## FIXME
552 if @data.include?('bar.width')
553 bar_width = @data['bar.width'].to_i
554 end
555 ### NOT YET ###
556 end
557 end
558
559
560 class Radar < Graph
561
562 WIDTH = 288
563 HEIGHT = 288
564
565 def initialize(dir, data, args: [])
566 super(dir, data, args)
567 end
568
569 def draw_frame(widget, cr)
570 frame_width = 2
571 if @data.include?('frame.width')
572 frame_width = @data['frame.width'].to_i
573 end
574 cr.set_line_width(frame_width)
575 cr.set_source_rgb(*get_color('frame'))
576 num = (@args.length / 2).to_i
577 for index_ in 0..num-1
578 x = (146 + (Math.cos(Math::PI * (0.5 - 2.0 * index_ / num)) * 114).to_i)
579 y = (146 - (Math.sin(Math::PI * (0.5 - 2.0 * index_ / num)) * 114).to_i)
580 cr.move_to(146, 146,)
581 cr.line_to(x, y)
582 cr.stroke()
583 end
584 end
585
586 def draw_graph(widget, cr)
587 num = (@args.length / 2).to_i
588 for index_ in 0..num-1
589 begin
590 value = @args[index_ * 2 + 1]
591 @args[index_ * 2 + 1] = Float(value)
592 rescue
593 @args[index_ * 2 + 1] = 0.0
594 end
595 if @args[index_ * 2 + 1] < 0
596 @args[index_ * 2 + 1] = 0.0
597 end
598 end
599 limit_min = @args[1]
600 for index_ in 0..num-1
601 if @args[index_ * 2 + 1] < limit_min
602 limit_min = @args[index_ * 2 + 1]
603 end
604 end
605 limit_max = @args[1]
606 for index_ in 0..num-1
607 if @args[index_ * 2 + 1] > limit_max
608 limit_max = @args[index_ * 2 + 1]
609 end
610 end
611 line_width = 2
612 if @data.include?('line.width')
613 line_width = @data['line.width'].to_i
614 end
615 cr.set_line_width(line_width)
616 cr.set_source_rgb(*get_color('line'))
617 if limit_max > 0
618 value = (@args[(num - 1) * 2 + 1] / limit_max)
619 else
620 value = 1.0
621 end
622 src_x = (146 + (Math.cos(
623 Math::PI * (0.5 - 2.0 * (num - 1) / num)) * value * 100).to_i)
624 src_y = (146 - (Math.sin(
625 Math::PI * (0.5 - 2.0 * (num - 1) / num)) * value * 100).to_i)
626 cr.move_to(src_x, src_y)
627 for index_ in 0..num-1
628 if limit_max > 0
629 value = (@args[index_ * 2 + 1] / limit_max)
630 else
631 value = 1.0
632 end
633 dst_x = (146 + (
634 Math.cos(Math::PI * (0.5 - 2.0 * index_ / num)) * value * 100).to_i)
635 dst_y = (146 - (
636 Math.sin(Math::PI * (0.5 - 2.0 * index_ / num)) * value * 100).to_i)
637 cr.line_to(dst_x, dst_y)
638 end
639 cr.stroke()
640 font_size = 9 # pixel
641 @font_desc.set_size(font_size * Pango::SCALE)
642 @layout.set_font_description(@font_desc)
643 cr.set_source_rgb(*get_color('font'))
644 for index_ in 0..num-1
645 ##if limit_max > 0
646 ## value = (@args[index_ * 2 + 1] / limit_max)
647 ##else
648 ## value = 1.0
649 ##end
650 value = 1.2 # XXX
651 x = (146 + (Math.cos(
652 Math::PI * (0.5 - 2.0 * index_ / num)) * value * 100).to_i)
653 y = (146 - (Math.sin(
654 Math::PI * (0.5 - 2.0 * index_ / num)) * value * 100).to_i)
655 @layout.set_text(@args[index_ * 2])
656 w, h = @layout.pixel_size
657 x -= (w / 2).to_i
658 y -= (h / 2).to_i
659 cr.move_to(x, y)
660 cr.show_pango_layout(@layout)
661 end
662 end
663 end
664
665
666 class Radar2 < Graph
667
668 WIDTH = 288
669 HEIGHT = 288
670
671 def initialize(dir, data, args: [])
672 super(dir, data, args)
673 end
674 end
675 end