From 941c0692565e5e9ae05cf8697eb6f0dd197afc8c Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Fri, 24 May 2019 15:10:44 +0200 Subject: [PATCH] examples/times_table: V 0.0.8 fixes --- examples/times_table/times_table.v | 47 +++++++++++++++--------------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/examples/times_table/times_table.v b/examples/times_table/times_table.v index cdd4452ca7..5c5ccfdded 100644 --- a/examples/times_table/times_table.v +++ b/examples/times_table/times_table.v @@ -2,24 +2,23 @@ import ui import gx const ( - WIN_SIZE = 540 - MIN = 1 - MAX = 9 - FONT_SIZE = 30 - N = MAX - MIN + 1 - CELL_SIZE = WIN_SIZE / N - TEXT_CFG = gx.TextCfg { color: gx.BLACK, size: FONT_SIZE } - HEADER_COLOR = gx.rgb(240, 240, 240) + WinSize = 540 + Min = 1 + Max = 9 + FontSize = 30 + N = Max - Min + 1 + CellSize = WinSize / N + TextCfg = gx.TextCfg { color: gx.BLACK, size: FontSize } + HeaderColor = gx.rgb(240, 240, 240) ) fn main() { - cfg := ui.WinCfg { - width: WIN_SIZE - height: WIN_SIZE + ui.new_window(ui.WinCfg { + width: WinSize + height: WinSize title: 'Times Table' draw_fn: draw - } - wnd := ui.new_window(cfg) + }) for { ui.wait_events() } @@ -27,29 +26,29 @@ fn main() { // ui.Window uses native drawing API (Core Graphics, GDI+) fn draw() { - gx.draw_rect(0, 0, WIN_SIZE, CELL_SIZE, HEADER_COLOR) // Horizontal header - gx.draw_rect(0, 0, CELL_SIZE, WIN_SIZE, HEADER_COLOR) // Vertical header - for i := MIN; i <= MAX; i++ { - y := CELL_SIZE * (i - MIN) - for j := MIN; j <= MAX; j++ { + ui.draw_rect(0, 0, WinSize, CellSize, HeaderColor)// Horizontal header + ui.draw_rect(0, 0, CellSize, WinSize, HeaderColor)// Vertical header + for i := Min; i <= Max; i++ { + y := CellSize * (i - Min) + for j := Min; j <= Max; j++ { // Skip top left corner - if i == MIN && j == MIN { + if i == Min && j == Min { continue } // Draw the result - x := CELL_SIZE * (j - MIN) + x := CellSize * (j - Min) res := i * j - mut text_padding_x := (CELL_SIZE - FONT_SIZE) / 2 - 1 + mut text_padding_x := (CellSize - FontSize) / 2 - 1 text_padding_y := text_padding_x - 3 if res < 10 { text_padding_x += 9 } - gx.draw_text(x + text_padding_x, y + text_padding_y, res.str(), TEXT_CFG) + ui.draw_text(x + text_padding_x, y + text_padding_y, res.str(), TextCfg) } // Horizontal lines - gx.draw_line(0, y, WIN_SIZE, y) + ui.draw_line(0, y, WinSize, y) // Vertical lines - gx.draw_line(y, 0, y, WIN_SIZE) + ui.draw_line(y, 0, y, WinSize) } }