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

math: move the math.mathutil generic min/max/abs fns to math (#13042)

This commit is contained in:
Delyan Angelov
2022-01-05 18:02:20 +02:00
committed by GitHub
parent de711da774
commit 59357e873d
24 changed files with 180 additions and 100 deletions

View File

@@ -1,7 +1,6 @@
import gg
import gx
import math
import math.mathutil as mu
import os
import rand
import time
@@ -563,7 +562,7 @@ fn (mut app App) resize() {
window_size := gg.window_size()
w := window_size.width
h := window_size.height
m := f32(mu.min(w, h))
m := f32(math.min(w, h))
app.ui.dpi_scale = s
app.ui.window_width = w
app.ui.window_height = h
@@ -587,7 +586,7 @@ fn (app &App) draw() {
xpad, ypad := app.ui.x_padding, app.ui.y_padding
ww := app.ui.window_width
wh := app.ui.window_height
m := mu.min(ww, wh)
m := math.min(ww, wh)
labelx := xpad + app.ui.border_size
labely := ypad + app.ui.border_size / 2
app.draw_tiles()
@@ -621,7 +620,7 @@ fn (app &App) draw_tiles() {
xstart := app.ui.x_padding + app.ui.border_size
ystart := app.ui.y_padding + app.ui.border_size + app.ui.header_size
toffset := app.ui.tile_size + app.ui.padding_size
tiles_size := mu.min(app.ui.window_width, app.ui.window_height) - app.ui.border_size * 2
tiles_size := math.min(app.ui.window_width, app.ui.window_height) - app.ui.border_size * 2
// Draw the padding around the tiles
app.gg.draw_rounded_rect_filled(xstart, ystart, tiles_size, tiles_size, tiles_size / 24,
app.theme.padding_color)
@@ -683,8 +682,8 @@ fn (app &App) draw_tiles() {
fn (mut app App) handle_touches() {
s, e := app.touch.start, app.touch.end
adx, ady := mu.abs(e.pos.x - s.pos.x), mu.abs(e.pos.y - s.pos.y)
if mu.max(adx, ady) < 10 {
adx, ady := math.abs(e.pos.x - s.pos.x), math.abs(e.pos.y - s.pos.y)
if math.max(adx, ady) < 10 {
app.handle_tap()
} else {
app.handle_swipe()
@@ -694,7 +693,7 @@ fn (mut app App) handle_touches() {
fn (mut app App) handle_tap() {
_, ypad := app.ui.x_padding, app.ui.y_padding
w, h := app.ui.window_width, app.ui.window_height
m := mu.min(w, h)
m := math.min(w, h)
s, e := app.touch.start, app.touch.end
avgx, avgy := avg(s.pos.x, e.pos.x), avg(s.pos.y, e.pos.y)
// TODO: Replace "touch spots" with actual buttons
@@ -732,12 +731,12 @@ fn (mut app App) handle_swipe() {
s, e := app.touch.start, app.touch.end
w, h := app.ui.window_width, app.ui.window_height
dx, dy := e.pos.x - s.pos.x, e.pos.y - s.pos.y
adx, ady := mu.abs(dx), mu.abs(dy)
dmin := if mu.min(adx, ady) > 0 { mu.min(adx, ady) } else { 1 }
dmax := if mu.max(adx, ady) > 0 { mu.max(adx, ady) } else { 1 }
adx, ady := math.abs(dx), math.abs(dy)
dmin := if math.min(adx, ady) > 0 { math.min(adx, ady) } else { 1 }
dmax := if math.max(adx, ady) > 0 { math.max(adx, ady) } else { 1 }
tdiff := int(e.time.unix_time_milli() - s.time.unix_time_milli())
// TODO: make this calculation more accurate (don't use arbitrary numbers)
min_swipe_distance := int(math.sqrt(mu.min(w, h) * tdiff / 100)) + 20
min_swipe_distance := int(math.sqrt(math.min(w, h) * tdiff / 100)) + 20
if dmax < min_swipe_distance {
return
}

View File

@@ -3,8 +3,8 @@
// Don't use this editor for any serious work.
// A lot of funtionality is missing compared to your favourite editor :)
import strings
import math.mathutil as mu
import os
import math
import term.ui as tui
enum Movement {
@@ -330,11 +330,11 @@ fn (mut b Buffer) move_cursor(amount int, movement Movement) {
}
}
.page_up {
dlines := mu.min(b.cursor.pos_y, amount)
dlines := math.min(b.cursor.pos_y, amount)
b.move_updown(-dlines)
}
.page_down {
dlines := mu.min(b.lines.len - 1, b.cursor.pos_y + amount) - b.cursor.pos_y
dlines := math.min(b.lines.len - 1, b.cursor.pos_y + amount) - b.cursor.pos_y
b.move_updown(dlines)
}
.left {