Coder Social home page Coder Social logo

kotlin-bindables's Introduction

kotlin-bindables

bindables library for kotlin inspired by the osu!framework bindable system

what are bindables

Bindables are objects that hold a value and provide following listeners:

  • Bindable
    • value change
  • BindableList
    • value added to list
    • value removed from list
    • value set by index in list
  • BindableMap
    • value set
    • value removed

why not use kotlin delegate?

To use delegate with decent syntax the library would have to do reflection which is not very good for performance

Installation

Kotlin DSL

repositories {
    maven {
        name = "devOS"
        url = uri("https://mvn.devos.one/releases")
    }
}

dependencies {
    implementation("cz.lukynka:kotlin-bindables:1.1")
}

Gradle Groovy

repositories {
  maven {
    name "devOS"
    url "https://mvn.devos.one/releases"
  }
}

dependencies {
  implementation 'cz.lukynka:kotlin-bindables:1.1'
}

Usage

There are different types of bindables, each with their own events

Bindable

// give initial value of 5
val playerHealth = Bindable<Int>(5)

// Register a listener
playerHealth.valueChanged {
    println("Player health changed from ${it.oldValue} to ${it.newValue}!")
}

// Set the value
playerHealth.value = 20

BindableList

val playersOnline = BindableList<String>()

// called when item gets added
playersOnline.itemAdded {
    println("Player ${it.item} has joined!")
}


// called when item gets removed
playersOnline.itemRemoved {
    println("Player ${it.item} has left!")
}

// called when item at index is set
playersOnline.itemChanged {
    println("object at index ${it.index} has been changed to ${it.item}!")
}

playersOnline.add("AsoDesu_")
playersOnline.add("LukynkaCZE")
playersOnline.remove("AsoDesu_")
playersOnline.setIndex(1, "ColleiImpact")

BindableMap

val uuidToPlayerName = BindableMap<UUID, String>()

// Is both called from BindableMap.set and BindableMap.add
uuidToPlayerName.itemSet {
    println("Player with uuid ${it.key} (${it.value}) has joined!")
}

// called when item is removed
uuidToPlayerName.itemRemoved {
    println("Player with uuid ${it.key} (${it.value}) has left!")
}

// called when the map is updated 
uuidToPlayerName.mapUpdated {
    println("The player list has been updated!")
}

uuidToPlayerName[UUID.fromString("aeb19a9c-a64a-4255-bb42-e74f05f9d30f")] = "AsoDesu_"
uuidToPlayerName[UUID.fromString("0c9151e4-7083-418d-a29c-bbc58f7c741b")] = "LukynkaCZE"
uuidToPlayerName.remove(UUID.fromString("aeb19a9c-a64a-4255-bb42-e74f05f9d30f"))
uuidToPlayerName.set(UUID.fromString("0c9151e4-7083-418d-a29c-bbc58f7c741b"), "ColleiImpact")

Extras

On all the types, you can also use .triggerUpdate() to call the listeners without actually causing any update to the value(s)

Both BindableList and BindableMap have .addIfNotPresent(value) and .removeIfPresent(value) functions

All of the bindable types provide .setSilently(value) which will set the value of the bindable without calling any of the listeners


kotlin-bindables's People

Contributors

lukynkacze avatar

Stargazers

Asoji avatar  avatar Skylar Redwood avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.