2021-09-07 17:36:50 +03:00
|
|
|
package rui
|
|
|
|
|
|
|
|
import "strings"
|
|
|
|
|
|
|
|
// DrawFunction is the constant for the "draw-function" property tag.
|
|
|
|
// The "draw-function" property sets the draw function of CanvasView.
|
|
|
|
// The function should have the following format: func(Canvas)
|
|
|
|
const DrawFunction = "draw-function"
|
|
|
|
|
|
|
|
// CanvasView interface of a custom draw view
|
|
|
|
type CanvasView interface {
|
|
|
|
View
|
|
|
|
Redraw()
|
|
|
|
}
|
|
|
|
|
|
|
|
type canvasViewData struct {
|
|
|
|
viewData
|
|
|
|
drawer func(Canvas)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCanvasView creates the new custom draw view
|
|
|
|
func NewCanvasView(session Session, params Params) CanvasView {
|
|
|
|
view := new(canvasViewData)
|
2022-09-01 11:04:50 +03:00
|
|
|
view.init(session)
|
2021-09-07 17:36:50 +03:00
|
|
|
setInitParams(view, params)
|
|
|
|
return view
|
|
|
|
}
|
|
|
|
|
|
|
|
func newCanvasView(session Session) View {
|
|
|
|
return NewCanvasView(session, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init initialize fields of ViewsContainer by default values
|
2022-09-01 11:04:50 +03:00
|
|
|
func (canvasView *canvasViewData) init(session Session) {
|
|
|
|
canvasView.viewData.init(session)
|
2021-09-07 17:36:50 +03:00
|
|
|
canvasView.tag = "CanvasView"
|
|
|
|
}
|
|
|
|
|
2022-05-22 12:54:02 +03:00
|
|
|
func (canvasView *canvasViewData) String() string {
|
|
|
|
return getViewString(canvasView)
|
|
|
|
}
|
|
|
|
|
2021-09-07 17:36:50 +03:00
|
|
|
func (canvasView *canvasViewData) normalizeTag(tag string) string {
|
|
|
|
tag = strings.ToLower(tag)
|
|
|
|
switch tag {
|
|
|
|
case "draw-func":
|
|
|
|
tag = DrawFunction
|
|
|
|
}
|
|
|
|
return tag
|
|
|
|
}
|
|
|
|
|
|
|
|
func (canvasView *canvasViewData) Remove(tag string) {
|
|
|
|
canvasView.remove(canvasView.normalizeTag(tag))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (canvasView *canvasViewData) remove(tag string) {
|
|
|
|
if tag == DrawFunction {
|
|
|
|
canvasView.drawer = nil
|
|
|
|
canvasView.Redraw()
|
2021-11-20 11:15:28 +03:00
|
|
|
canvasView.propertyChangedEvent(tag)
|
2021-09-07 17:36:50 +03:00
|
|
|
} else {
|
|
|
|
canvasView.viewData.remove(tag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-26 18:36:00 +03:00
|
|
|
func (canvasView *canvasViewData) Set(tag string, value any) bool {
|
2021-09-07 17:36:50 +03:00
|
|
|
return canvasView.set(canvasView.normalizeTag(tag), value)
|
|
|
|
}
|
|
|
|
|
2022-07-26 18:36:00 +03:00
|
|
|
func (canvasView *canvasViewData) set(tag string, value any) bool {
|
2021-09-07 17:36:50 +03:00
|
|
|
if tag == DrawFunction {
|
|
|
|
if value == nil {
|
|
|
|
canvasView.drawer = nil
|
|
|
|
} else if fn, ok := value.(func(Canvas)); ok {
|
|
|
|
canvasView.drawer = fn
|
|
|
|
} else {
|
|
|
|
notCompatibleType(tag, value)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
canvasView.Redraw()
|
2021-11-20 11:15:28 +03:00
|
|
|
canvasView.propertyChangedEvent(tag)
|
2021-09-07 17:36:50 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return canvasView.viewData.set(tag, value)
|
|
|
|
}
|
|
|
|
|
2022-07-26 18:36:00 +03:00
|
|
|
func (canvasView *canvasViewData) Get(tag string) any {
|
2021-09-07 17:36:50 +03:00
|
|
|
return canvasView.get(canvasView.normalizeTag(tag))
|
|
|
|
}
|
|
|
|
|
2022-07-26 18:36:00 +03:00
|
|
|
func (canvasView *canvasViewData) get(tag string) any {
|
2021-09-07 17:36:50 +03:00
|
|
|
if tag == DrawFunction {
|
|
|
|
return canvasView.drawer
|
|
|
|
}
|
|
|
|
return canvasView.viewData.get(tag)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (canvasView *canvasViewData) htmlTag() string {
|
|
|
|
return "canvas"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (canvasView *canvasViewData) Redraw() {
|
|
|
|
if canvasView.drawer != nil {
|
|
|
|
canvas := newCanvas(canvasView)
|
|
|
|
canvas.ClearRect(0, 0, canvasView.frame.Width, canvasView.frame.Height)
|
|
|
|
if canvasView.drawer != nil {
|
|
|
|
canvasView.drawer(canvas)
|
|
|
|
}
|
2022-11-02 20:08:02 +03:00
|
|
|
canvas.finishDraw()
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (canvasView *canvasViewData) onResize(self View, x, y, width, height float64) {
|
|
|
|
canvasView.viewData.onResize(self, x, y, width, height)
|
|
|
|
canvasView.Redraw()
|
|
|
|
}
|
|
|
|
|
|
|
|
// RedrawCanvasView finds CanvasView with canvasViewID and redraws it
|
|
|
|
func RedrawCanvasView(rootView View, canvasViewID string) {
|
|
|
|
if canvas := CanvasViewByID(rootView, canvasViewID); canvas != nil {
|
|
|
|
canvas.Redraw()
|
|
|
|
}
|
|
|
|
}
|