1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

examples/times_table: V 0.0.8 fixes

This commit is contained in:
Alexander Medvednikov 2019-05-24 15:10:44 +02:00
parent a439faf605
commit 941c069256

View File

@ -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)
}
}