Skip to content

Add opacity support for gui.Panel #270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion gui/panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ type Panel struct {
paddingsColor math32.Color4 // panel padding color
contentColor math32.Color4 // panel content color
textureValid float32 // texture valid flag (bool)
dummy [3]float32 // complete 8 * vec4
opacity float32
dummy [2]float32 // complete 8 * vec4
}
}

Expand Down Expand Up @@ -135,6 +136,7 @@ func (p *Panel) Initialize(ipan IPanel, width, height float32) { // TODO rename

p.width = width
p.height = height
p.udata.opacity = 1

// If first time, create panel quad geometry
if panelQuadGeometry == nil {
Expand Down Expand Up @@ -393,6 +395,22 @@ func (p *Panel) Paddings() RectBounds {
return p.paddingSizes
}

// SetOpacity sets the alpha modifier of this panel
func (p *Panel) SetOpacity(alpha float32) {
if alpha > 1 {
alpha = 1
}
if alpha < 0 {
alpha = 0
}
p.udata.opacity = alpha
}

// Opacity returns current opacity value
func (p *Panel) Opacity() float32 {
return p.udata.opacity
}

// SetBordersColor sets the color of this panel borders
// The borders opacity is set to 1.0 (full opaque)
func (p *Panel) SetBordersColor(color *math32.Color) {
Expand Down
4 changes: 4 additions & 0 deletions renderer/shaders/panel_fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ uniform vec4 Panel[8];
#define PaddingColor Panel[5] // panel padding color
#define ContentColor Panel[6] // panel content color
#define TextureValid bool(Panel[7].x) // texture valid flag
#define Opacity Panel[7].y // panel alpha modifier

// Output
out vec4 FragColor;
Expand Down Expand Up @@ -103,18 +104,21 @@ void main() {
}

FragColor = color;
FragColor.a *= Opacity;
return;
}

// Checks if fragment is inside paddings area
if (checkRect(Padding)) {
FragColor = PaddingColor;
FragColor.a *= Opacity;
return;
}

// Checks if fragment is inside borders area
if (checkRect(Border)) {
FragColor = BorderColor;
FragColor.a *= Opacity;
return;
}

Expand Down
4 changes: 4 additions & 0 deletions renderer/shaders/sources.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions tools/g3nshaders/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"log"
"os"
"path/filepath"
"sort"
"strings"
"text/template"
)
Expand Down Expand Up @@ -163,6 +164,10 @@ func processDir(dir string, include bool) {
panic(err)
}

sort.Slice(finfos, func(i, j int) bool {
return finfos[i].Name() < finfos[j].Name()
})

// Process all directory entries.
for _, fi := range finfos {
if fi.IsDir() {
Expand Down