start to add Languages in Settings

This commit is contained in:
cuu
2018-12-19 20:31:32 +08:00
parent 8ee8d42cb5
commit 078d0279ed
12 changed files with 873 additions and 4 deletions

View File

@@ -85,4 +85,10 @@ func init() {
if MyIconPool == nil {
MyIconPool = NewIconPool()
}
if MyLangManager == nil {
MyLangManager = NewLangManager()
MyLangManager.Init()
}
}

View File

@@ -17,7 +17,7 @@ type InfoPageListItem struct {
func NewInfoPageListItem() *InfoPageListItem {
p := &InfoPageListItem{}
p.Height = 30
p.Height = DefaultInfoPageListItemHeight
p.ReadOnly = false
p.Labels = make(map[string]LabelInterface)
p.Icons = make( map[string]IconItemInterface)

View File

@@ -25,6 +25,7 @@ type LabelInterface interface {
SetText(text string)
Draw()
DrawCenter(bold bool)
SetBold(b bool)
}
type Label struct {
@@ -33,12 +34,14 @@ type Label struct {
FontObj *ttf.Font
Color *color.Color
CanvasHWND *sdl.Surface
Bold bool
// TextSurf *sdl.Surface
}
func NewLabel() *Label {
l := &Label{}
l.Color = &color.Color{83,83,83,255}
l.Bold = false
return l
}
@@ -90,6 +93,10 @@ func (self *Label) SetText(text string) {
self.Width,self.Height = font.Size(self.FontObj, self.Text)
}
func (self *Label) SetBold(b bool) {
self.Bold = b
}
func (self *Label) DrawCenter(bold bool) { // default bold is false
font.SetBold(self.FontObj,bold)
my_text := font.Render(self.FontObj,self.Text, true, self.Color, nil)
@@ -100,7 +107,7 @@ func (self *Label) DrawCenter(bold bool) { // default bold is false
}
func (self *Label) Draw() {
font.SetBold(self.FontObj,false) // avoing same font tangling set_bold to others
font.SetBold(self.FontObj,self.Bold) // avoing same font tangling set_bold to others
my_text := font.Render(self.FontObj,self.Text, true, self.Color, nil)

173
sysgo/UI/lang_manager.go Normal file
View File

@@ -0,0 +1,173 @@
package UI
import (
"fmt"
"strings"
"io/ioutil"
"path/filepath"
"github.com/veandco/go-sdl2/ttf"
"github.com/go-ini/ini"
)
func sliceToInt(s []int) int {
res := 0
op := 1
for i := len(s) - 1; i >= 0; i-- {
res += s[i] * op
op *= 10
}
return res
}
func ParseNum(s string) []int {
nLen := 0
for i := 0; i < len(s); i++ {
if b := s[i]; '0' <= b && b <= '9' {
nLen++
}
}
var n = make([]int, 0, nLen)
for i := 0; i < len(s); i++ {
if b := s[i]; '0' <= b && b <= '9' {
n = append(n, int(b)-'0')
}
}
return n
}
func GetNumberFromString(s string) int {
is := ParseNum(s)
return sliceToInt(is)
}
type LangManager struct {
Langs map[string]string
ConfigFilename string
CJKMode bool
}
func NewLangManager() *LangManager {
p := &LangManager{}
p.ConfigFilename = "00_English.ini"
p.CJKMode = false
return p
}
func (self *LangManager) Init() {
if self.Langs == nil {
self.SetLangs()
}
}
func (self *LangManager) UpdateLang() {
self.Langs = nil
self.SetLangs()
}
func (self *LangManager) IsCJKMode() bool {
var latins = [1]string{"English"}
self.CJKMode= false
for _,v := range latins {
if strings.HasPrefix(self.ConfigFilename,v) {
self.CJKMode = false
break
}
}
return self.CJKMode
}
func (self *LangManager) SetLangs() {
self.Langs = make(map[string]string)
fname := "sysgo/.lang"
load_opts := ini.LoadOptions{
IgnoreInlineComment:true,
}
if FileExists(fname) {
config_bytes,err := ioutil.ReadFile(fname)
if err == nil {
self.ConfigFilename = strings.Trim(string(config_bytes),"\r\n ")
if len(self.ConfigFilename) < 3 {
self.ConfigFilename = "00_English.ini"
}
}
}else {
System("touch " + fname)
}
config_file_relative_path := filepath.Join("sysgo","langs",self.ConfigFilename)
if FileExists(config_file_relative_path) == false {
return
}
//no matter what ,we must have 00_English.ini
cfg, err := ini.LoadSources(load_opts, config_file_relative_path)
if err != nil {
fmt.Printf("Fail to read file: %v\n", err)
return
}
section := cfg.Section("Langs")
if section != nil {
opts := section.KeyStrings()
for _,v := range opts {
self.Langs[v] = section.Key(v).String()
}
}
}
func (self *LangManager) Tr(english_key_str string) string {
if self.Langs == nil {
return english_key_str
}
if len(self.Langs) == 0 {
return english_key_str
}
if v,ok := self.Langs[english_key_str]; ok {
return v
}
return english_key_str
}
func (self *LangManager) TrFont(orig_font_str string) *ttf.Font {
font_size_number := GetNumberFromString(orig_font_str)
if font_size_number > 120 {
panic("font string format error")
}
if strings.Contains(self.ConfigFilename,"English.ini") {
return Fonts[orig_font_str]
}else {
if font_size_number > 28 {
panic("cjk font size over 28")
}
}
return Fonts[ fmt.Sprintf("notosanscjk%d",font_size_number) ]
}
var MyLangManager *LangManager

View File

@@ -21,6 +21,7 @@ type MultiLabel struct {
CanvasHWND *sdl.Surface
//TextSurf *sdl.Surface
MaxWidth int
Bold bool
}
func NewMultiLabel() *MultiLabel{
@@ -28,7 +29,7 @@ func NewMultiLabel() *MultiLabel{
l.Color = &color.Color{83,83,83,255}
l.Width = 135
l.Height = 100
l.Bold = false
return l
}
@@ -66,12 +67,17 @@ func (self *MultiLabel) SetText(text string) {
}
func (self *MultiLabel) SetBold(b bool) {
self.Bold = b
}
func (self *MultiLabel) DrawCenter(bold bool) {
}
func (self *MultiLabel) Draw() {
font.SetBold(self.FontObj,false) // avoing same font tangling set_bold to others
font.SetBold(self.FontObj,self.Bold) // avoing same font tangling set_bold to others
self.blit_text(self.CanvasHWND, self.Text,self.PosX,self.PosY,self.FontObj)
}

View File

@@ -174,6 +174,9 @@ type PageInterface interface {
GetAlign() int
SetAlign(al int)
ScrollUp()
ScrollDown()
SetIconIndex(idx int)
GetIconIndex() int
@@ -1013,3 +1016,52 @@ func (self *Page) GetAlign() int {
return self.Align
}
func (self *Page) ScrollUp() {
if len(self.MyList) == 0 {
return
}
self.PsIndex -=1
if self.PsIndex < 0 {
self.PsIndex = 0
}
cur_li := self.MyList[self.PsIndex]
x,y := cur_li.Coord()
_,h := cur_li.Size()
if y < 0 {
for i,_ := range self.MyList{
x,y = self.MyList[i].Coord()
_, h = self.MyList[i].Size()
self.MyList[i].NewCoord(x,y + h)
}
//self.Scrolled +=1
}
}
func (self *Page) ScrollDown() {
if len(self.MyList) == 0 {
return
}
self.PsIndex +=1
if self.PsIndex >= len(self.MyList) {
self.PsIndex = len(self.MyList) - 1
}
cur_li := self.MyList[self.PsIndex]
x,y := cur_li.Coord()
_,h := cur_li.Size()
if y+ h > self.Height {
for i,_ := range self.MyList{
x,y = self.MyList[i].Coord()
_, h = self.MyList[i].Size()
self.MyList[i].NewCoord(x,y - h)
}
// self.Scrolled -=1
}
}